package com.cjst.controller.base;

import java.util.Date;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.pojo.base.AlarmResult;
import com.cjst.pojo.part.Parts;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.MapUtils;
import com.cjst.service.base.AlarmResultService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.code.PageUtil;
import com.cjst.util.common.DateUtil;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/alarm")
public class AlarmResultController extends BaseController{
	
	@Value("${file.path}")
    private String filePath;
	
	
	@Autowired
	private AlarmResultService alarmResultService;
	@RequestMapping("")
	public String toAlarm(){
		return "base/alarm/alarm";
	}
	@RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer type,String alarmType) {
		WherePrams wherePrams=Method.where("delFlag", C.EQ, 0);
		if(type!=null){
			wherePrams.and("type", C.EQ, type);
		}
		if(alarmType!=null && alarmType.length()>0){
			wherePrams.and("alarmType", C.LIKE, alarmType,1);
		}
		wherePrams.orderBy(" create_time desc");
		PageUtil<AlarmResult> pageUtil = alarmResultService.listPageCount(wherePrams);
        return super.packForBT(pageUtil);
	}
	
	
	@RequestMapping(value="/view_detail/{id}")
	@RequiresPermissions("alarm_detail")
	public String detail(@PathVariable String id, Model model) {
		WherePrams where = Method.where("id", C.EQ, id);
        AlarmResult alarmResult = alarmResultService.get(where);
        
        alarmResult.setImgUrl(filePath+alarmResult.getImgUrl());
        if (alarmResult != null) {
            Map<String, Object> alarmResultMap = MapUtils.transBean2Map(alarmResult);
            model.addAttribute("alarm", alarmResultMap);
        }
		
		return "base/alarm/alarm_detail";
	}
	
	
	@RequestMapping(value="/edit/{id}")
	@RequiresPermissions("alarm_edit")
	public String edit(@PathVariable String id, Model model) {
		WherePrams where = Method.where("id", C.EQ, id);
        AlarmResult alarmResult = alarmResultService.get(where);
        if (alarmResult != null) {
            Map<String, Object> alarmResultMap = MapUtils.transBean2Map(alarmResult);
            model.addAttribute("alarm", alarmResultMap);
        }
		
		return "base/alarm/alarm_edit";
	}
	
	
	@RequestMapping(value="/reply/{id}")
	@RequiresPermissions("alarm_reply")
	public String reply(@PathVariable String id, Model model) {
		WherePrams where = Method.where("id", C.EQ, id);
        AlarmResult alarmResult = alarmResultService.get(where);
        if (alarmResult != null) {
            Map<String, Object> alarmResultMap = MapUtils.transBean2Map(alarmResult);
            model.addAttribute("alarm", alarmResultMap);
        }
		
		return "base/alarm/alarm_reply";
	}
	
	
	@RequestMapping(value = "/save/updateOne", method = RequestMethod.POST)
    @ResponseBody
    public Object updateOne(AlarmResult alarm) {
		if (alarm == null) {
            return new ResultObj(1, "参数传递错误，修改失败", null);
        }
        WherePrams where = Method.where("id", C.EQ, alarm.getId());
        AlarmResult alarmResult = alarmResultService.get(where);
        if (alarmResult == null) {
            return new ResultObj(1, "根据Id未查询到部件信息，修改失败", null);
        }
      
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        alarmResult.setUserId(sysUser.getId());

        int flag = alarmResultService.updateLocal(alarm);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	
	
	@RequestMapping(value = "/save/reply", method = RequestMethod.POST)
    @ResponseBody
    public Object reply(AlarmResult alarm) {
		if (alarm == null) {
            return new ResultObj(1, "参数传递错误，修改失败", null);
        }
        WherePrams where = Method.where("id", C.EQ, alarm.getId());
        AlarmResult alarmResult = alarmResultService.get(where);
        if (alarmResult == null) {
            return new ResultObj(1, "根据Id未查询到部件信息，修改失败", null);
        }
      
        alarmResult.setResultsContent(alarm.getResultsContent());
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        alarmResult.setUserId(sysUser.getId());
        alarmResult.setUpdateTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        
        int flag = alarmResultService.updateLocal(alarm);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	
	
	
}












